#include "ErrorThread.h"

#define SIGNAL_SEC_TIMEOUT		1

ErrorThread::ErrorThread(QObject *parent) : QThread(parent)
{
    stopped = true;
    ocrStatus = true;
    scannerStatus = true;

    gpioPort = GpioPort::getInstance();
}

void ErrorThread::run()
{
    stopped = false;
    bool previousOcrStatus = false;
    bool previousScannerStatus = false;

    while(!stopped) {
        if(gpioPort->getState(5, &ocrStatus))
            continue;

        if(!ocrStatus != previousOcrStatus) {
            emit ocrStatusChanged(!ocrStatus);
            previousOcrStatus = !previousOcrStatus;
            sleep(SIGNAL_SEC_TIMEOUT);
        }

        if(gpioPort->getState(4, &scannerStatus))
            continue;

        if(!scannerStatus != previousScannerStatus) {
            emit scannerStatusChanged(!scannerStatus);
            previousScannerStatus = !previousScannerStatus;
            sleep(SIGNAL_SEC_TIMEOUT);
        }
    }
}

void ErrorThread::stop()
{
    stopped = true;
}
